/**
 *
 */
package com.py.credit.controller;

import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.DebugLogger;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.AuScontent;
import com.py.credit.service.AuScontentService;
import com.py.credit.service.SSearchService;
import com.py.credit.service.ScontentService;
import com.py.credit.service.ShareLogService;
import com.py.credit.utils.DateUtil;
import com.py.credit.utils.IpUtil;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author dd
 *
 */
@RestController
@RequestMapping(value = "/query")
public class SharingContentQuery {

    @Autowired
    private ShareLogService shareLogService;

    @Autowired
    ScontentService sContentService;
    @Autowired
    AuScontentService auScontentService;

    @Autowired
    private SSearchService sSearchService;

    @IgnoreAuthority
    @RequestMapping(value = "/JKSQ/getDetail.json")
    public RestServiceResult<Object> getDetail(
            @RequestParam(value = "kId", defaultValue = "", required = true) String kId,
            @RequestParam(value = "searchWord", defaultValue = "", required = true) String searchWord,
            @RequestParam(value = "token", defaultValue = "", required = true) String token,
            HttpServletRequest request) {

        if (StringUtils.isBlank(token)) {

            throw new ServiceException("token 丢失");

        }

        AuScontent auScontent = auScontentService.getByToken(token);

        if (auScontent == null) {

            throw new ServiceException("token:【" + token + "】未授权,请联系管理员");

        }

        String ipAddr = IpUtil.getInstance().getIpAddr();

        DebugLogger.debug("客户端ip:" + ipAddr);

        if (!ipAddr.equals(auScontent.getIp())) {

            throw new ServiceException("ip:【" + ipAddr + "】未授权,请联系管理员");

        }

        if (auScontent.getEndTime() != null) {

            try {

                String now = DateUtil.getInstance().dateToStr(new Date(), DateUtil.YYYY_MM_DD);
                Date nowDate = DateUtil.getInstance().strToDate(now, DateUtil.YYYY_MM_DD);
                if (nowDate.after(DateUtil.getInstance().strToDate(auScontent.getEndTime(), DateUtil.YYYY_MM_DD))) {

                    throw new ServiceException("token:【" + token + "】授权已过期,请重新申请");

                }
            } catch (ParseException e) {

                ExceptionLogger.error(e);
                throw new ServiceException("日期解析异常");

            }

        }

        String tybm = sSearchService.getTYBM(kId, searchWord);

        Map<String, Object> detail = sContentService.getDetail(kId, tybm, "JKSQ", null);

        shareLogService.add(3, (String) detail.get("category"), HttpServletHelper.getGuestIP(request), null, null, 0, searchWord);

        return new RestServiceResult<Object>(detail.get("results"));

    }

    /**
     * @api {get} /query/tyshxydm/getTableList.json
     * @apiName getTyshxydmTableList
     * @apiVersion 0.0.1
     * @apiGroup 共享查询
     *
     * @apiParam {String} condition 查询条件(选填)
     * @apiParam {int} limit 分页行数(选填)
     * @apiParam {int} page 页数(选填)
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {Object[]} data 结果集
     *
     * @apiSuccessExample {json} 成功返回结果
     *                    {"code":0,"msg":null,"count":4,"data":[{"A000001":"公积金公司1","A000002":"123456","A000003":"33","A000004":"33232","A000005":"23"},{"A000001":"荣利纺织品加工有限公司","A000002":"91220523594","A000003":"","A000004":"","A000005":""},{"A000001":"金久旺矿产品经销有限公司","A000002":"912205235","A000003":"","A000004":"","A000005":""},{"A000001":"亿鑫煤炭经销有限公司","A000002":"6985236","A000003":"","A000004":"","A000005":""}]}
     *
     */
    @Deprecated
    @RequestMapping(value = "/tyshxydm/getTableList.do", method = RequestMethod.GET)
    public RestServiceResult<List<Map<String, Object>>> getTyshxydmTableList(
            @RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page) {
        List<Map<String, Object>> tableList = sSearchService.getTyshxydmTableListForPage(condition, page);
        return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

    }

    /**
     * @api {get} /query/xyxx/getTableList.do
     * @apiName getXyxxTableList
     * @apiVersion 0.0.1
     * @apiGroup 共享查询
     *
     * @apiParam {String} condition 查询条件(选填)
     * @apiParam {int} limit 分页行数(选填)
     * @apiParam {int} page 页数(选填)
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {Object[]} data 结果集
     *
     * @apiSuccessExample {json} 成功返回结果
     *                    {"code":0,"msg":null,"count":20,"data":[{"TYPE":"FR","tybm":"1431b1036f6045d59dc85074413c39d8","name":"公积金公司1","number":"33"},{"TYPE":"FR","tybm":"455a29ddbbcc4914b8552664f3b71b1e","name":"公积金公司3","number":""},{"TYPE":"FR","tybm":"5929c9eff9e44707b1ba0e726f6e37d7","name":"亿鑫煤炭经销有限公司","number":""},{"TYPE":"FR","tybm":"724847c8e0bd468f81c44af12cd53575","name":"彼岸大酒店有限公司1","number":""},{"TYPE":"FR","tybm":"8afe16d924c14993a335191c270626ee","name":"企业社保1","number":""},{"TYPE":"FR","tybm":"8f0e48aea4ca44e98ac4317fd5cbb67a","name":"滋味餐饮有限公司","number":"222426200100133"},{"TYPE":"FR","tybm":"8f92518e1f594a11a343febb14014e43","name":"财园村酒店有限公司1","number":""},{"TYPE":"FR","tybm":"946e27cd9d494a2fa764283612efdb60","name":"公积金公司4","number":""},{"TYPE":"FR","tybm":"bdf293e3ed954df4b4aa146162e43caf","name":"财源酒店有限公司","number":""},{"TYPE":"FR","tybm":"c17d20b9595d477d95388b42a9ba0ff2","name":"宏大煤炭经销有限公司","number":""},{"TYPE":"FR","tybm":"c4284ec31f944b2e94076d9dca91be4c","name":"财园村酒店有限公司","number":"222426200103888"},{"TYPE":"FR","tybm":"c77197f4fcdd4d00abcd99de92742f45","name":"兴诚矿产品经销有限公司","number":""},{"TYPE":"FR","tybm":"f5ee225eb52141039aa382590133c032","name":"彼岸大酒店有限公司","number":"222426000013476"},{"TYPE":"ZRR","tybm":"19a762074e7b4bbc9a5a8ab6653b0d89","name":"王萌","number":"321083199006234816"},{"TYPE":"ZRR","tybm":"19a762074e7b4bbc9a5a8ab6653b0d90","name":"李海","number":"321083199006234817"}]}
     */
    @Deprecated
    @RequestMapping(value = "/xyxx/getTableList.do", method = RequestMethod.GET)
    public RestServiceResult<List<Map<String, Object>>> getXyxxTableList(
            @RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page) {
        List<Map<String, Object>> tableList = sSearchService.getXyxxTableListForPage(condition, page);
        return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

    }

    /**
     * @api {get} /query/xyxx/getDetail.json
     * @apiName getXyxxTableList
     * @apiVersion 0.0.1
     * @apiGroup 共享查询
     *
     * @apiParam {String} tybm 查询条件(选填)
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {Object[]} data 结果集
     *
     * @apiSuccessExample {json} 成功返回结果
     *                    {"code":0,"msg":null,"count":1,"data":[{}}
     */
    @Deprecated
    @RequestMapping(value = "/xyxx/getDetail.do", method = RequestMethod.GET)
    public RestServiceResult<Map<String, List<Map<String, Object>>>> getXyxxDetail(
            @RequestParam(value = "tybm", defaultValue = "", required = true) String tybm,
            @RequestParam(value = "token", defaultValue = "", required = true) String token,
            @RequestParam(value = "type", defaultValue = "", required = true) String type) {

        if (StringUtils.isBlank(tybm)) {

            throw new ServiceException("参数 tybm 不能为空");

        }

        // if (StringUtils.isBlank(token)) {

        // throw new ServiceException("token 丢失!");

        // }

        // AuScontent auScontent = auScontentService.getByToken(token);

        // if (auScontent == null) {

        // throw new ServiceException("token:【" + token + "】未授权");

        // }

        // String ipAddr = IpUtil.getInstance().getIpAddr();

        // DebugLogger.debug("客户端ip:" + ipAddr);

        // DebugLogger.debug("授权ip:" + auScontent.getIp());

        // if (!ipAddr.equals(auScontent.getIp())) {

        // throw new ServiceException("ip:【" + ipAddr + "】未授权");

        // }

        // if (auScontent.getEndTime() != null) {

        // try {
        // if ((new Date())
        // .after(DateUtil.getInstance().strToDate(auScontent.getEndTime(),
        // DateUtil.YYYY_MM_DD))) {

        // throw new ServiceException("token:【" + token + "】授权已过期");

        // }
        // } catch (ParseException e) {

        // ExceptionLogger.error(e);

        // }

        // }

        Map<String, List<Map<String, Object>>> detail = sSearchService.getXyxxDetail(tybm, type);

        return new RestServiceResult<Map<String, List<Map<String, Object>>>>(detail);

    }

    @Deprecated
    @RequestMapping(value = "/sgs/getTableList.do", method = RequestMethod.GET)
    public RestServiceResult<List<Map<String, Object>>> getSgsTableList(
            @RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
            @RequestParam(value = "type", defaultValue = "XK", required = true) String type) {
        List<Map<String, Object>> tableList = sSearchService.getSgsXxTableListByTypeForPage(condition, page, type);
        return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

    }

    @Deprecated
    @RequestMapping(value = "/sgs/getDetail.do", method = RequestMethod.GET)
    public RestServiceResult<Map<String, List<Map<String, Object>>>> getDetail(
            @RequestParam(value = "type", defaultValue = "", required = true) String type,
            @RequestParam(value = "name", defaultValue = "", required = true) String name) {

        if (StringUtils.isBlank(type)) {
            throw new ServiceException("参数 type 不能为空");
        }

        Map<String, List<Map<String, Object>>> tableList = sSearchService.getSgsXxDetail(type, name);

        return new RestServiceResult<Map<String, List<Map<String, Object>>>>(tableList);

    }

}
